package com.visionet.school.app.web.web.tclass;

import com.visionet.school.app.web.common.utils.PageInfo;
import com.visionet.school.app.web.entity.tclass.ClassNotice;
import com.visionet.school.app.web.entity.tclass.vo.ClassNoticeVo;
import com.visionet.school.app.web.service.tclass.ClassNoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dzd on 2017/3/2.
 */
@RequestMapping("/notice")
@Controller
public class ClassNoticeController {

    @Autowired
    private ClassNoticeService classNoticeService;

    @RequestMapping(value = "/wechat/findClassNotice", method = RequestMethod.GET)
    public ResponseEntity<?> findClassNotice(String id){
        Map<String,Object> map = new HashMap<>();
        if(id.isEmpty()){
            return new ResponseEntity(map , HttpStatus.NOT_ACCEPTABLE);
        }
        ClassNotice classNotice = classNoticeService.findClassById(id);
        map.put("classNotice",classNotice);
        return new ResponseEntity(map , HttpStatus.OK);
    }

    @RequestMapping(value = "/wechat/getNoticePage", method = RequestMethod.POST)
    public ResponseEntity<?> getNoticePage(@RequestBody ClassNoticeVo classNoticeVo){
        if(classNoticeVo == null || (StringUtils.isBlank(classNoticeVo.getClassId()) && StringUtils.isBlank(classNoticeVo.getClassId()))){
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }
        if(classNoticeVo.getPageInfo() == null){
            classNoticeVo.setPageInfo(new PageInfo());
        }
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> pageResult;
        try {
            pageResult = classNoticeService.getNoticePage(classNoticeVo,"createDate");
            map.put("pageResult",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }


    @RequestMapping(value = "/wechat/saveNoticePage", method = RequestMethod.POST)
    public ResponseEntity<?> saveNoticePage(@RequestBody ClassNoticeVo classNoticeVo){
        if(classNoticeVo == null || StringUtils.isBlank(classNoticeVo.getClassId()) || StringUtils.isBlank(classNoticeVo.getNoticeTitle())
                || StringUtils.isBlank(classNoticeVo.getNoticeContent())){
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }
        Map<String,Object> map = new HashMap<>();
        try {
            classNoticeService.save(classNoticeVo);
            map.put("stat","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }

}
